package com.gking.centerSystem.service.AiServiceRecord;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gking.centerSystem.common.ProgramException;
import com.gking.centerSystem.common.Result;
import com.gking.centerSystem.entity.AiServiceRecord;
import com.gking.centerSystem.entity.dto.PagingQueryAiServiceRecordDto;
import com.gking.centerSystem.mapper.AiServiceRecordMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Slf4j
@Service
public class AiServiceRecordServiceImpl implements AiServiceRecordService {

    @Resource
    private AiServiceRecordMapper aiServiceRecordMapper;

    @Override
    public void addRecord(AiServiceRecord aiServiceRecord) {
        try {
            aiServiceRecordMapper.insert(aiServiceRecord);
        } catch (Exception e) {
            log.error("添加记录失败", e);
            throw new ProgramException();
        }
    }

    @Override
    public Result<Page> getRecordList(Integer page, Integer size, PagingQueryAiServiceRecordDto conditionDto) {
        try {
            if (page == null || size == null) return Result.error("请将参数补充完整");

            Page<PagingQueryAiServiceRecordDto> pageInfo = new Page<>(page, size);
            String account = conditionDto.getAccount();
            String nickname = conditionDto.getNickname();
            String userId = conditionDto.getAccessUserId();
            String userMessage = conditionDto.getUserMessage();
            String description = conditionDto.getDescription();
            Integer responseStatus = conditionDto.getResponseStatus();

            IPage<PagingQueryAiServiceRecordDto> aiServiceRecordByCriteria = aiServiceRecordMapper.findAiServiceRecordByCriteria(
                    pageInfo, account, nickname, userId, userMessage, description, responseStatus);

            Page<PagingQueryAiServiceRecordDto> pageResult = (Page<PagingQueryAiServiceRecordDto>) aiServiceRecordByCriteria;
            pageResult.setRecords(aiServiceRecordByCriteria.getRecords());

            return Result.success("分页条件获取AI服务记录成功！", pageResult);
        } catch (Exception e) {
            log.error("分页条件获取AI服务记录失败", e);
            throw new ProgramException();
        }
    }

}
